﻿using System.Reflection;
using Yu.Cms.Common;
using Yu.Cms.Config;


namespace Yu.Cms.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)  
    /// DataCache类在导出代码的文件夹里
    /// <appSettings>  
    /// <add key="DAL" value="Yu.Cms.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess
    {
        private static string AssemblyPath
        {
            get { return BaseConfigs.GetDAL; }
        }
        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }
        /// <summary>
        /// 创建Admin数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.IAdmin CreateAdmin()
        {

            string ClassNamespace = AssemblyPath + ".Admin";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.IAdmin)objType;
        }

        /// <summary>
        /// 创建album数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.Ialbum Createalbum()
        {

            string ClassNamespace = AssemblyPath + ".album";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.Ialbum)objType;
        }


        /// <summary>
        /// 创建albumType数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.IalbumType CreatealbumType()
        {

            string ClassNamespace = AssemblyPath + ".albumType";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.IalbumType)objType;
        }


        /// <summary>
        /// 创建Annonce数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.IAnnonce CreateAnnonce()
        {

            string ClassNamespace = AssemblyPath + ".Annonce";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.IAnnonce)objType;
        }


        /// <summary>
        /// 创建BigType数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.IBigType CreateBigType()
        {

            string ClassNamespace = AssemblyPath + ".BigType";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.IBigType)objType;
        }


        /// <summary>
        /// 创建Book数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.IBook CreateBook()
        {

            string ClassNamespace = AssemblyPath + ".Book";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.IBook)objType;
        }


        /// <summary>
        /// 创建Comment数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.IComment CreateComment()
        {

            string ClassNamespace = AssemblyPath + ".Comment";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.IComment)objType;
        }


        /// <summary>
        /// 创建Content数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.IContent CreateContent()
        {

            string ClassNamespace = AssemblyPath + ".Content";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.IContent)objType;
        }



        /// <summary>
        /// 创建SmallType数据层接口
        /// </summary>
        public static Yu.Cms.IDAL.ISmallType CreateSmallType()
        {

            string ClassNamespace = AssemblyPath + ".SmallType";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.ISmallType)objType;
        }

        /// <summary>
        /// 创建Tags数据层接口。
        /// </summary>
        public static Yu.Cms.IDAL.ITags CreateTags()
        {
            string ClassNamespace = AssemblyPath + ".Tags";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Yu.Cms.IDAL.ITags)objType;
        }
    }
}
